<template>
</template>
<script>
import juheh5Service from '../../../services/juheh5Service';

export default {
    props: {
        modelValue: {
            type: Object,
            default: () => ({}),
        },
        toApkCacheId: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            myValue: this.modelValue,
            account: '',
            token: '',
        };
    },
    mounted() {
        this.getToApkAccount()
    },
    watch: {
        modelValue: {
            deep: true,
            handler(newVal) {
                this.myValue = newVal;
                this.curTab = newVal.currentComponentTab;
            },
        },
        myValue: {
            deep: true,
            handler(newVal) {
                this.handleSyncData();
            },
        },
    },
    methods: {
        getToApkAccount() {
            const _this = this
            axios.post('/api/h5/toapk/token_account', {
                toapk_cache_id: this.toApkCacheId,
                game_id: this.myValue.gameId
            }).then(res => {
                let data = res.data;
                if (data.code != 0) {
                    layer.msg(data.msg, { time: 1000 });
                    return false;
                }

                let info = data.data;

                // 如果存在转端数据，则弹出转端账号
                if (info.hasOwnProperty('account')) {
                    _this.account = info.account
                    _this.token = info.token
                    // 直接调用登录
                    _this.handleLoginClick()
                // 如果不存在，则正常弹出登录
                } else {
                    let localAccountInfo = JSON.parse(localStorage.getItem('account_info')) || []
                    if (localAccountInfo.length === 0) {
                        this.myValue.currentComponent = 'Login'
                    } else {
                        this.myValue.currentComponent = 'AccountSelectLogin'
                    }
                }
            }).catch((error) => {
                layer.msg(error, { time: 1000 });
            });
        },
        handleLoginClick() {
            if (this.account !== '' && this.token !== '') {
                axios.post('/api/juheh5/account/token_login', { account: this.account, token: this.token, game_id: this.myValue.gameId }).then(res => {
                    let data = res.data;
                    if (data.code != 0) {
                        layer.msg(data.msg, { time: 1000 });
                        return false;
                    }

                    let info = data.data;
                    let localAccountInfo = JSON.parse(localStorage.getItem('account_info')) || [];
                    localAccountInfo.forEach((item, index) => {
                        if (item.account === info.account) {
                            localAccountInfo.splice(index, 1);
                        }
                    });
                    localAccountInfo.unshift(info);
                    localStorage.setItem('account_info', JSON.stringify(localAccountInfo));

                    juheh5Service.loginCb(info)
                    return
                }).catch((error) => {
                    layer.msg(error, { time: 1000 });
                });
            } else {
                layer.msg('请选择登录账号', { time: 1000 });
            }

            return false;
        },
        handleSyncData() {
            this.$emit("update:modelValue", this.myValue);
        },
    },
};
</script>
